import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    //路由软搭载方式
    component: () => import('../components/Home.vue'),
    // 路由跳转 配置打开页面默认为Welcome.vue
    redirect: '/welcome',
    // 配置mian区域显示内容的子路由
    children: [
      { path: '/welcome', component: () => import('../components/index/Welcome.vue') },
      { path: '/users', component: () => import('../components/user/Users.vue') },
      { path: '/rights', component: () => import('../components/power/Rights.vue') },
      { path: '/roles', component: () => import('../components/power/Roles.vue') },
      { path: '/categories', component: () => import('../components/goods/Cate.vue') },
      { path: '/params', component: () => import('../components/goods/Params.vue') },
      { path: '/goods', component: () => import('../components/goods/List.vue') },
      { path: '/goods/add', component: () => import('../components/goods/Add.vue') },
      { path: '/goods/edit', component: () => import('../components/goods/Edit.vue') },
      { path: '/orders', component: () => import('../components/order/Orders.vue') },
    ]
  }
]

const router = new VueRouter({
  routes
})

// 配置路由守卫
router.beforeEach((to, from, next) => {
  // 如果访问登录页面，则方行
  if (to.path === '/login') {
    return next()
  }
  // 如果用户未登录，则跳转到/login
  const userInfo = JSON.parse(sessionStorage.getItem('userInfo'))
  if (!userInfo) {
    return next('/login')
  }
  // 如果用户已登录，则放行
  next()
})

export default router
